﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MIT.SysAdmin
{
    public partial class SysAdminMsg : System.Web.UI.Page
    {
        String Invite1;
        String Invite2;
        String Invite3;
        String Sender1;
        String Sender2;
        String Sender3;
        String Division1;
        String Division2;
        String Division3;
        String ImageUser1;
        String ImageUser2;
        String ImageUser3;
        String GlobalUsername;
        String GlobalPosition;
        String GlobalName;

        String Username1;
        String Email1;
        String request1;
        String request2;
        String request3;

        String sub1;
        String sub2;
        String sub3;

        String Username2;
        String Email2;

        String Username3;
        String Email3;

        int intno1;
        int intno2;
        int intno3;

        String Req1;
        String Req2;
        String Req3;
        String NewsDate1;
        String NewsDate2;
        String NewsDate3;
        String NewsTitle1;
        String NewsTitle2;
        String NewsTitle3;
        String NewsDetails1;
        String NewsDetails2;
        String NewsDetails3;

        String Intersub1;
        String Intersub2;
        String Intersub3;



        protected void Page_Load(object sender, EventArgs e)
        {
            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            GlobalUsername = username;
            GlobalPosition = Position;
            GlobalName = Name;
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqCom3 = new SqlCommand("spNotifications", con);
                ReqCom3.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqCom3.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqCom3.ExecuteReader();

                if (drreq3.Read())
                {
                    Invite1 = Convert.ToString(drreq3["REQUEST1"]);
                    Invite2 = Convert.ToString(drreq3["REQUEST2"]);
                    Invite3 = Convert.ToString(drreq3["REQUEST3"]);
                    Sender1 = Convert.ToString(drreq3["SENDER1"]);
                    Sender2 = Convert.ToString(drreq3["SENDER2"]);
                    Sender3 = Convert.ToString(drreq3["SENDER3"]);
                    Division1 = Convert.ToString(drreq3["DIVISION1"]);
                    Division2 = Convert.ToString(drreq3["DIVISION2"]);
                    Division3 = Convert.ToString(drreq3["DIVISION3"]);

                }
            }

            Session["CalLoggingInfo"] = Name;
            Session["CalLoggingPosition"] = Position;
            Session["CalLoggingInfoUsername"] = username;

            //////////////////////////////////////////Profile Image Extraction//////////////////////////////////
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser1", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender1);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser1 = Convert.ToString(drreq3["username"]);


                }
            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser2", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender2);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser2 = Convert.ToString(drreq3["username"]);


                }
            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser3", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender3);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser3 = Convert.ToString(drreq3["username"]);


                }
            }

            if (ImageUser1 != null)
            {
                ImageProf1.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser1;

            }
            if (ImageUser2 != null)
            {
                ImageProf2.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser2;
            }
            if (ImageUser3 != null)
            {
                ImageProf3.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser3;
            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////

            ///////////////////Notification Number//////////////////////////////////////////////////


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews1", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News1 = new SqlParameter("@News1", 1);
                GetNews.Parameters.Add(News1);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate1 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle1 = Convert.ToString(dr["Title"]);
                    NewsDetails1 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews2", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News2 = new SqlParameter("@News2", 2);
                GetNews.Parameters.Add(News2);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate2 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle2 = Convert.ToString(dr["Title"]);
                    NewsDetails2 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews3", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News3 = new SqlParameter("@News3", 3);
                GetNews.Parameters.Add(News3);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate3 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle3 = Convert.ToString(dr["Title"]);
                    NewsDetails3 = Convert.ToString(dr["Details"]);
                }

            }



            LabelDetails1.Text = NewsDetails1;
            LabelDetails2.Text = NewsDetails2;
            LabelDetails3.Text = NewsDetails3;

            LabelNews1.Text = NewsTitle1;
            LabelNews2.Text = NewsTitle2;
            LabelNews3.Text = NewsTitle3;

            LabelDate1.Text = NewsDate1;
            LabelDate2.Text = NewsDate2;
            LabelDate3.Text = NewsDate3;

            LabelSender1.Text = Sender1;
            LabelSender2.Text = Sender2;
            LabelSender3.Text = Sender3;
            LabelInvite1.Text = Invite1;
            LabelInvite2.Text = Invite2;
            LabelInvite3.Text = Invite3;
            LabelDivision1.Text = Division1;
            LabelDivision2.Text = Division2;
            LabelDivision3.Text = Division3;

            if (LabelSender1.Text == "")
            {
                LabelMsgNo.Text = "";
                ImageProf1.Visible = false;
                ImageProf2.Visible = false;
                ImageProf3.Visible = false;
                LabelSender1.Visible = false;
                LabelSender2.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite1.Visible = false;
                LabelInvite2.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision1.Visible = false;
                LabelDivision2.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply1.Visible = false;
                ButtonReply2.Visible = false;
                ButtonReply3.Visible = false;


            }
            else if (LabelSender2.Text == "")
            {
                LabelMsgNo.Text = "1";
                ImageProf2.Visible = false;
                ImageProf3.Visible = false;
                LabelSender2.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite2.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision2.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply2.Visible = false;
                ButtonReply3.Visible = false;
            }
            else if (LabelSender3.Text == "")
            {
                LabelMsgNo.Text = "2";
                ImageProf3.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply3.Visible = false;
            }
            else
            {
                LabelMsgNo.Text = "3";
            }
        }


        protected void ButtonReply1_Click(object sender, EventArgs e)
        {
            ButtonCannot1.Visible = true;
            ButtonCan1.Visible = true;
        }
        protected void ButtonReply2_Click(object sender, EventArgs e)
        {
            ButtonCan2.Visible = true;
            ButtonCannot2.Visible = true;
        }
        protected void ButtonReply3_Click(object sender, EventArgs e)
        {
            ButtonCan3.Visible = true;
            ButtonCannot3.Visible = true;
        }

        protected void ButtonCan1_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                sub1 = LabelInvite1.Text.Substring(30, 4);
                Intersub1 = LabelInvite1.Text.Substring(0, 5);
                intno1 = Convert.ToInt32(sub1);
                SqlCommand cmd = new SqlCommand("spAddInterChecker1", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@InterviewerCheck1", GlobalName);
                cmd.Parameters.AddWithValue("@InterviewNo1", intno1);
                cmd.Parameters.AddWithValue("@InterviewMode1", Intersub1);
                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg1", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }
            Response.Redirect("SysAdminMsg.aspx");
        }

        protected void ButtonCannot1_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {


                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                /////////////////User 1////////////////////////////////////////////////////////////////////////////////////


                SqlCommand com = new SqlCommand("spGetName1", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@Name1", Sender1);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                if (dr.Read())
                {
                    Username1 = dr["USERNAME"].ToString();
                }
                con.Close();


                SqlCommand com2 = new SqlCommand("spGetEmail1", con);
                com2.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname2 = new SqlParameter("@UserName1", Username1);
                com2.Parameters.Add(sendname2);



                con.Open();
                SqlDataReader dr1;

                dr1 = com2.ExecuteReader();

                if (dr1.Read())
                {
                    Email1 = dr1["EMAIL"].ToString();
                }
                /////////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker1", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", Username1);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request1 = drreq1["REQUEST1"].ToString();
                }
                ///////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom2 = new SqlCommand("spRequestchecker2", con);
                ReqCom2.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName2 = new SqlParameter("@username", Username1);
                ReqCom2.Parameters.Add(ReqName2);



                con.Open();
                SqlDataReader drreq2;

                drreq2 = ReqCom2.ExecuteReader();

                if (drreq2.Read())
                {
                    request2 = drreq2["REQUEST2"].ToString();
                }
                //////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom3 = new SqlCommand("spRequestchecker3", con);
                ReqCom3.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", Username1);
                ReqCom3.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqCom3.ExecuteReader();

                if (drreq3.Read())
                {
                    request3 = drreq3["REQUEST3"].ToString();

                }

                if (request1 == "")
                {
                    sub1 = LabelInvite1.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest1", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UsernameReq1", Username1);
                    cmd.Parameters.AddWithValue("@Sender1", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION1", GlobalPosition);
                    cmd.Parameters.AddWithValue("@Req1", "Interview No. " + sub1 + ". Invitation is rejected. ");
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request2 == "")
                {
                    sub2 = LabelInvite1.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest2", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req2", "Interview No. " + sub2 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender2", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION2", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq2", Username1);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request3 == "")
                {
                    sub3 = LabelInvite1.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest3", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req3", "Interview No. " + sub3 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender3", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION3", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq3", Username1);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                else
                {
                    LabelRequestMax.Text = Sender1 + " has too many requests. ";
                    goto EndofPage;
                }


                //////////////////////////////////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////////////////////////////////

                if (Page.IsValid)
                    try
                    {
                        MailMessage mm = new MailMessage("cms.mit2015@gmail.com", Email1);
                        //mm.From = new MailAddress();
                        //mm.To.Add();
                        mm.Subject = "Interview Panel Invitation Rejection";

                        mm.Body = "Hello Test 1";

                        mm.IsBodyHtml = true;

                        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                        smtpClient.EnableSsl = true;
                        smtpClient.Credentials = new System.Net.NetworkCredential()
                        {
                            UserName = "cms.mit2015@gmail.com",
                            Password = "millenium2015"
                        };
                        smtpClient.Send(mm);


                    }

                    catch (Exception ex)
                    {
                        Response.Write("Could not send the e-mail - error: " + ex.Message);
                    }


                con.Close();



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg1", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }

        EndofPage: ;
        Response.Redirect("SysAdminMsg.aspx");
        }

        protected void ButtonCannot2_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {


                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                /////////////////User 1////////////////////////////////////////////////////////////////////////////////////


                SqlCommand com = new SqlCommand("spGetName2", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@Name2", Sender2);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                if (dr.Read())
                {
                    Username2 = dr["USERNAME"].ToString();
                }
                con.Close();


                SqlCommand com2 = new SqlCommand("spGetEmail2", con);
                com2.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname2 = new SqlParameter("@UserName2", Username2);
                com2.Parameters.Add(sendname2);



                con.Open();
                SqlDataReader dr1;

                dr1 = com2.ExecuteReader();

                if (dr1.Read())
                {
                    Email2 = dr1["EMAIL"].ToString();
                }
                /////////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker1", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", Username2);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request1 = drreq1["REQUEST1"].ToString();
                }
                ///////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom2 = new SqlCommand("spRequestchecker2", con);
                ReqCom2.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName2 = new SqlParameter("@username", Username2);
                ReqCom2.Parameters.Add(ReqName2);



                con.Open();
                SqlDataReader drreq2;

                drreq2 = ReqCom2.ExecuteReader();

                if (drreq2.Read())
                {
                    request2 = drreq2["REQUEST2"].ToString();
                }
                //////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom3 = new SqlCommand("spRequestchecker3", con);
                ReqCom3.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", Username2);
                ReqCom3.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqCom3.ExecuteReader();

                if (drreq3.Read())
                {
                    request3 = drreq3["REQUEST3"].ToString();

                }

                if (request1 == "")
                {
                    sub1 = LabelInvite2.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest1", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UsernameReq1", Username2);
                    cmd.Parameters.AddWithValue("@Sender1", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION1", GlobalPosition);
                    cmd.Parameters.AddWithValue("@Req1", "Interview No. " + sub1 + ". Invitation is rejected. ");
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request2 == "")
                {
                    sub2 = LabelInvite2.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest2", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req2", "Interview No. " + sub2 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender2", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION2", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq2", Username2);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request3 == "")
                {
                    sub3 = LabelInvite2.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest3", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req3", "Interview No. " + sub3 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender3", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION3", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq3", Username2);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                else
                {
                    LabelRequestMax.Text = Sender1 + " has too many requests. ";
                    goto EndofPage;
                }


                //////////////////////////////////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////////////////////////////////

                if (Page.IsValid)
                    try
                    {
                        MailMessage mm = new MailMessage("cms.mit2015@gmail.com", Email2);
                        //mm.From = new MailAddress();
                        //mm.To.Add();
                        mm.Subject = "Interview Panel Invitation Rejection";

                        mm.Body = "Hello Test 1";

                        mm.IsBodyHtml = true;

                        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                        smtpClient.EnableSsl = true;
                        smtpClient.Credentials = new System.Net.NetworkCredential()
                        {
                            UserName = "cms.mit2015@gmail.com",
                            Password = "millenium2015"
                        };
                        smtpClient.Send(mm);


                    }

                    catch (Exception ex)
                    {
                        Response.Write("Could not send the e-mail - error: " + ex.Message);
                    }


                con.Close();



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg2", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }

        EndofPage: ;
        Response.Redirect("SysAdminMsg.aspx");

        }

        protected void ButtonCannot3_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {


                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                /////////////////User 1////////////////////////////////////////////////////////////////////////////////////


                SqlCommand com = new SqlCommand("spGetName3", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@Name3", Sender3);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                if (dr.Read())
                {
                    Username3 = dr["USERNAME"].ToString();
                }
                con.Close();


                SqlCommand com2 = new SqlCommand("spGetEmail3", con);
                com2.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname2 = new SqlParameter("@UserName3", Username3);
                com2.Parameters.Add(sendname2);



                con.Open();
                SqlDataReader dr1;

                dr1 = com2.ExecuteReader();

                if (dr1.Read())
                {
                    Email3 = dr1["EMAIL"].ToString();
                }
                /////////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker1", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", Username3);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request1 = drreq1["REQUEST1"].ToString();
                }
                ///////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom2 = new SqlCommand("spRequestchecker2", con);
                ReqCom2.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName2 = new SqlParameter("@username", Username3);
                ReqCom2.Parameters.Add(ReqName2);



                con.Open();
                SqlDataReader drreq2;

                drreq2 = ReqCom2.ExecuteReader();

                if (drreq2.Read())
                {
                    request2 = drreq2["REQUEST2"].ToString();
                }
                //////////////////////////////////////////////////////////////////////////////////////////////
                con.Close();


                SqlCommand ReqCom3 = new SqlCommand("spRequestchecker3", con);
                ReqCom3.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", Username3);
                ReqCom3.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqCom3.ExecuteReader();

                if (drreq3.Read())
                {
                    request3 = drreq3["REQUEST3"].ToString();

                }

                if (request1 == "")
                {
                    sub1 = LabelInvite1.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest1", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UsernameReq1", Username3);
                    cmd.Parameters.AddWithValue("@Sender1", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION1", GlobalPosition);
                    cmd.Parameters.AddWithValue("@Req1", "Interview No. " + sub1 + ". Invitation is rejected. ");
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request2 == "")
                {
                    sub2 = LabelInvite2.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest2", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req2", "Interview No. " + sub2 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender2", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION2", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq2", Username3);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                else if (request3 == "")
                {
                    sub3 = LabelInvite3.Text.Substring(30, 4);
                    SqlCommand cmd = new SqlCommand("spAddRequest3", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Req3", "Interview No. " + sub3 + ". Invitation is rejected. ");
                    cmd.Parameters.AddWithValue("@Sender3", GlobalName);
                    cmd.Parameters.AddWithValue("@DIVISION3", GlobalPosition);
                    cmd.Parameters.AddWithValue("@UsernameReq3", Username3);
                    con.Close();
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                else
                {
                    LabelRequestMax.Text = Sender1 + " has too many requests. ";
                    goto EndofPage;
                }


                //////////////////////////////////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////////////////////////////////

                if (Page.IsValid)
                    try
                    {
                        MailMessage mm = new MailMessage("cms.mit2015@gmail.com", Email3);
                        //mm.From = new MailAddress();
                        //mm.To.Add();
                        mm.Subject = "Interview Panel Invitation Rejection";

                        mm.Body = "Hello Test 1";

                        mm.IsBodyHtml = true;

                        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                        smtpClient.EnableSsl = true;
                        smtpClient.Credentials = new System.Net.NetworkCredential()
                        {
                            UserName = "cms.mit2015@gmail.com",
                            Password = "millenium2015"
                        };
                        smtpClient.Send(mm);


                    }

                    catch (Exception ex)
                    {
                        Response.Write("Could not send the e-mail - error: " + ex.Message);
                    }


                con.Close();



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg3", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }

        EndofPage: ;
        Response.Redirect("SysAdminMsg.aspx");
        }
        protected void ButtonCan2_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                sub2 = LabelInvite1.Text.Substring(30, 4);
                intno2 = Convert.ToInt32(sub2);
                Intersub2 = LabelInvite1.Text.Substring(0, 5);
                SqlCommand cmd = new SqlCommand("spAddInterChecker2", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@InterviewerCheck2", GlobalName);
                cmd.Parameters.AddWithValue("@InterviewNo2", intno2);
                cmd.Parameters.AddWithValue("@InterviewMode2", Intersub2);
                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg2", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }
            Response.Redirect("SysAdminMsg.aspx");
        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Login/SignIn.aspx");
        }
        protected void ButtonCan3_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                sub3 = LabelInvite3.Text.Substring(30, 4);
                intno3 = Convert.ToInt32(sub3);
                Intersub3 = LabelInvite1.Text.Substring(0, 5);
                SqlCommand cmd = new SqlCommand("spAddInterChecker3", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@InterviewerCheck3", GlobalName);
                cmd.Parameters.AddWithValue("@InterviewNo3", intno3);
                cmd.Parameters.AddWithValue("@InterviewMode3", Intersub3);
                con.Close();
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg3", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();

                //if (dr.Read())
                //{
                //    name1 = dr["USERNAME"].ToString();
                //}
                con.Close();
            }
            Response.Redirect("SysAdminMsg.aspx");
        }
    }
}